<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登录</title>
    <link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/layui.css">
    <script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
    <style>
        body {
            background: url("/static/imgs/background_login.jpg") no-repeat fixed center;
        }
    </style>
</head>

<body>
    <div style="position: absolute;left: 50%;top: 50%;width: 400px;margin-left: -200px;margin-top: -200px;">
        <div style="background: #ffffff;padding: 20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444;">
            <div class="layui-form-item">
                <div class="layui-form-item" style="color: gray">
                    <h2>财务管理系统</h2>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" id="username" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                    <div class="layui-input-block">
                        <input type="password" id="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" onclick="dologin()">登录</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        layui.use(['layer'], function() {
            $ = layui.jquery;
            layer = layui.layer;

            //用户名组件获取焦点
            $('#username').focus();

            //回车登录
            $('input').keydown(function(e) {
                if (e.keyCode === 13) {
                    dologin();
                }
            });
        });


        //登录
        function dologin() {
            var username = $.trim($('#username').val());
            var password = $.trim($('#password').val());
            if (username === '') {
                layer.alert('请输入用户名', {
                    icon: 2
                });
                return;
            }
            if (password === '') {
                layer.alert('请输入密码', {
                    icon: 2
                });
                return;
            }

            $.post('/login/dologin.html', {
                'username': username,
                'password': password
            }, function(res) {
                if (res.code > 0) {
                    console.log(res);
                    layer.msg(res.msg, { time: 1000 });
                } else {
                    layer.msg(res.msg);
                    console.log(res);
                    setTimeout(function() {
                        window.location.href = '/index.html';
                    }, 500);
                }
            }, 'json');
        }
    </script>
</body>

</html>